<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* $Id: flowM.php 53 2012-04-03 15:02:56Z zengdong615@gmail.com $
*/
/**
* 信息流	
*/		
class FlowM extends CI_Model
{
	private $_type = array('short' => 0 , 'blog' => 1, 'demand' => 2 ); // 信息类别

	function __construct()
	{
		parent::__construct();
	}

	function add($flow='')
	{
		if (empty($flow)) {
			return FALSE;
		}else{
			if ($flow['cid'] == $this->_type['short']) {
				$rid = $this->add_short($flow['txt']);
				if ($rid) {
					$v = array('from_id' =>$flow['from_id'] , 
							'to_id' => $flow['to_id'], 
							'cid' => $flow['cid'], 
							'rid' => $rid,
							'create_time'  => time());
					$this->db->insert('flow', $v);

					$this->db->select_max('id','id');
					$query = $this->db->get('flow');
					$row = $query->result();
					$row = $row[0];

					return $row->id;
				}
			}
		}

		return FALSE;
	}

	function add_short($value='',$seq = 1)
	{
		if (empty($value)) {
			return FALSE;
		}else{
			$this->db->insert('flow_cnt', array('htm' => $value, 'seq' => $seq));
			$this->db->select_max('id','id');
			$query = $this->db->get('flow_cnt');
			$row = $query->result();
			$row = $row[0];

			return $row->id;
		}
	}

	function get_all($uid='', $limit=20, $offset = 0)
	{
		if (empty($uid)) {
			return NULL;
		}else{
			$this->db->select('a.*');
			$this->db->distinct();
			$this->db->from('flow AS a');
			$this->db->from('bridge AS b');
			$w = '(b.to_id = '.$uid.' AND b.from_id = a.from_id) OR a.from_id ='.$uid; 
			$this->db->where($w);
			$this->db->order_by("create_time", "desc"); 
			$this->db->limit($limit, $offset);
			$query = $this->db->get();
			$row = $query->result();

			if (!empty($row)) {
				return 	$row;
			}
		}

		return NULL;
	}

	function get_cnt($rid='')
	{
		$row = NULL;
		if (!empty($rid)) {
			$this->db->select('htm');
			$query = $this->db->get_where('flow_cnt',array('id' => $rid, 'seq' => 1));

			$row = $query->result_array();
			if ($row) {
				$row = $row[0]['htm'];
			}
		}

		return $row;
	}
}